package com.yiqi.liebang.feature.home.view;


import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.rxbus2.RxBus;
import com.luck.picture.lib.rxbus2.Subscribe;
import com.luck.picture.lib.rxbus2.ThreadMode;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.BaseFragment;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.util.EventEntity;
import com.yiqi.liebang.entity.bo.QuestionBo;
import com.yiqi.liebang.feature.home.contract.QuestionContract;
import com.yiqi.liebang.feature.home.dependencies.question.DaggerQuestionComponent;
import com.yiqi.liebang.feature.home.dependencies.question.QuestionPresenterModule;
import com.yiqi.liebang.feature.home.view.adapter.QuestionAdapter;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class QuestionFragment  extends BaseFragment implements QuestionContract.View, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, BaseQuickAdapter.OnItemChildClickListener {

    @BindView(R.id.rv_search)
    RecyclerView mRvSearch;

    @Inject
    QuestionContract.Presenter mPresenter;
    QuestionFragment.CallBackValue callBackValue;
    private QuestionAdapter mAdapter;
    private EmptyView mEmptyView;
    private int pageIndex = 1;
    private int pageSize = 10;
    List<QuestionBo> mData;

    private String mContent;


    /**
     * fragment与activity产生关联是  回调这个方法
     */
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        //当前fragment从activity重写了回调接口  得到接口的实例化对象
        callBackValue = (QuestionFragment.CallBackValue) getActivity();
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onResume() {
        super.onResume();
        callBackValue.SendMessageValue(true);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_question;
    }

    @Override
    protected void initInjector() {
        DaggerQuestionComponent.builder().questionPresenterModule(new QuestionPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        super.initView();
        if (!RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().register(this);
        }
        initAdapter();
    }

    public void initAdapter() {
        mEmptyView = new EmptyView(mRvSearch);
        mRvSearch.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvSearch.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new QuestionAdapter();
        mAdapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_LEFT);
        mAdapter.bindToRecyclerView(mRvSearch);
        mAdapter.setEmptyView(mEmptyView.getEmptyView("请输入搜索关键词！"));
        mAdapter.setNewData(null);
        mAdapter.setOnLoadMoreListener(this, mRvSearch);
        mAdapter.setOnItemClickListener(this);
        mAdapter.setOnItemChildClickListener(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventBus(EventEntity obj) {
        switch (obj.what) {
            case 1:
                mContent = obj.medias;
                pageIndex = 1;
                mPresenter.searchQuestion(mContent, pageIndex, pageSize);
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

        if (RxBus.getDefault().isRegistered(this)) {
            RxBus.getDefault().unregister(this);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvent() {

    }

    @Override
    public void showQuestionListSuccess(List<QuestionBo> workBoPageData) {
        mData = workBoPageData;
        setData(true, mData);
        mAdapter.setEnableLoadMore(true);
    }

    private void loadMore() {
        mPresenter.searchQuestionMore(mContent, pageIndex, pageSize);
    }

    private void setData(boolean isRefresh, List data) {
        pageIndex++;
        final int size = data == null ? 0 : data.size();
        if (isRefresh) {
            mAdapter.setNewData(data);
        } else {
            if (size > 0) {
                mAdapter.addData(data);
            }
        }
        if (size < pageSize) {
            mAdapter.loadMoreEnd(isRefresh);
        } else {
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void showQuestionListError(String error) {
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
        mAdapter.setEnableLoadMore(true);
    }

    @Override
    public void showQuestionListMoreSuccess(List<QuestionBo> msg) {
        setData(false, msg);
    }

    @Override
    public void showQuestionListMoreError(String msg) {
        mAdapter.loadMoreFail();
    }

    @Override
    public void showQuestionListEmpty() {
        mAdapter.setEmptyView(mEmptyView.getEmptyView("暂无搜索到匹配的问答"));
        mAdapter.setNewData(null);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(getActivity(), AskDetailActivity.class);
        intent.putExtra("answer_id", mAdapter.getData().get(position).getId());
        startActivity(intent);
    }

    @Override
    public void onLoadMoreRequested() {
        loadMore();
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        if(mAdapter.getData().get(position).getDataPrivacyType()==0){
            Intent intent = new Intent(getActivity(), UserInfoActivity.class);
            intent.putExtra("is_view_my", false);
            intent.putExtra("visitorUid", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
            startActivity(intent);
        }
    }

    //定义一个回调接口
    public interface CallBackValue {
        public void SendMessageValue(boolean strValue);
    }
}
